//获取应用实例
var app = getApp();

// 初始化数据
var init = {

  // 数据
  data: {

  },

  // 页面加载
  onLoad: function (options) {
    console.log(options);
    this.setData({
      options: options,
      addrType: options && options.addr_id ? 'edit' : 'add'
    })
    var that = this;
    this.setData({
      options: options
    })
    // 适应多种页面传参
    var referer = '';
    for (var i in options) {
      referer += '&' + i + '=' + options[i];
    }
    that.setData({
      referer: referer.substr(1)
    })
  },

  // 提交
  submit: function () {

    var that = this;
    var options = that.data.options;
    console.log()
    // 用户输入校验
    var virify = that.virifi();

    if (virify != '') {
      wx.showModal({
        title: '提示',
        content: virify,
        showCancel: false
      });
      return
    };

    // 授权
    app.getLoginSession(function (session) {

      var params = {};
      if (that.data.addrType == 'edit') { // 修改
        params = {
          app: 'my_address',
          act: 'edit_address',
          addr_id: options.addr_id,
          consignee: options.consignee,
          region_name: options.region_name,
          address: options.address,
          phone_mob: options.phone_mob,
          is_default: options.is_default,
          t_session: session
        }
      } else if (that.data.addrType == 'add') { // 增加
        params = {
          app: 'my_address',
          act: 'add_address',
          consignee: options.consignee,
          region_name: options.region_name,
          address: options.address,
          phone_mob: options.phone_mob,
          is_default: options.is_default,
          t_session: session
        }
      }

      wx.request({

        // 请求地址
        url: app.api,

        // 请求参数
        data: params,

        // 请求成功
        success: function (res) {
          console.log(res.data);
          var data = res.data;

          if (data.done && data.retval) { // 成功
            if (options['from']=='confirmOrder'){
              wx.redirectTo({
                url: '/pages/address/address?' + that.data.referer,
              })
            }else{
              wx.redirectTo({
                url: '/pages/address/address?from=' + options['from']
              })
            }
            } else { // 失败
            that.setData({
              errorTips: data.msg || '请求错误，act：index'
            })
          }

        }
      })
    })
  },

  // 设置默认
  setDefault: function (event) {
    var is_default = event.currentTarget.dataset.is_default;
    var options = this.data.options;
    console.log(options);
    this.setData({
      'options.is_default': options.is_default == 1 ? 0 : 1
    })
  },

  // 实时获取用户输入
  input: function (event) {
    var that = this;
    var value = event.detail.value;
    var type = event.currentTarget.dataset.type;

    if (type == 'consignee') {
      this.setData({
        'options.consignee': value
      })
    } else if (type == 'phone_mob') {
      this.setData({
        'options.phone_mob': value
      })
    } else if (type == 'region_name') {
      this.setData({
        'options.region_name': value
      })
    } else if (type == 'address') {
      this.setData({
        'options.address': value
      })
    }
  },

  // 用户输入验证
  virifi: function () {
    var that = this;
    var error = '';
    var options = that.data.options;
    console.log(options);

    if (!options.consignee || options.consignee == '') {
      error = '请输入收货人';
    } else if (!options.phone_mob || options.phone_mob == '' || !/^1[34578][0-9]{9}$/.test(options.phone_mob)) {
      error = '请输入正确的手机号码';
    } else if (!options.region_name || options.region_name == '') {
      error = '请输入省市镇';
    } else if (!options.address || options.address == '') {
      error = '请输入详细地址';
    }
    return error;
  }

};

// 注册页面
Page(init);
